package com.kaka.controller;

import com.kaka.dao.UserDao;
import sun.java2d.pipe.SpanShapeRenderer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name = "AddUserServlet", urlPatterns = "/AddUserServlet")
public class AddUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");//中文处理
        try {

            //接收参数
            String username = request.getParameter("username");
            int age = Integer.parseInt(request.getParameter("age"));
            Date birth = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("birth"));

            UserDao dao = new UserDao();
            dao.save(username,age,birth);

            //转发到列表页面
            request.getRequestDispatcher("/UserServlet").forward(request,response);

        }catch (Exception e){
            e.printStackTrace();
        }



    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
